module.exports = {
  format(time = Date.now(), formatStr = "YYYY-MM-DD hh:mm:ss", ms = true) {
    if (!time) return;
    const t = new Date(ms ? time : time * 1000);
    const tf = i => (i < 10 ? `0${i}` : i);
    return formatStr.replace(/YYYY|MM|DD|hh|mm|ss/g, a => {
      switch (a) {
        case "YYYY":
          return tf(t.getFullYear());
        case "MM":
          return tf(t.getMonth() + 1);
        case "mm":
          return tf(t.getMinutes());
        case "DD":
          return tf(t.getDate());
        case "hh":
          return tf(t.getHours());
        case "ss":
          return tf(t.getSeconds());
      }
    });
  }
};
